// pages/cart/cart.js
import {
    Invite
} from 'invitePeopleList-model.js'
var invite = new Invite();
//获取应用实例
const app = getApp()

Page({
    data: {
        orderList: [],
        noneDate: false,
        hasMore: false,
        page: 1,
        startTime: '',
        endTime: ''
    },
    onLoad: function (options) {
        let date = new Date();
        this.setData({
            startTime: invite.formate(date.getTime(), 'yyyy-MM-dd'),
            endTime: invite.formate(date.getTime(), 'yyyy-MM-dd'),
        })
        this.commonSearch('1', '')

    },
    onPullDownRefresh() {
        this.setData({
            page: 1
        })

        this.commonSearch('1', 'pulldown')
    },
    onReachBottom() {
        var {
            page,
            hasMore,
            searchVal
        } = this.data;
        /**
         * 此处加上数据大小的判断
         */
        if (hasMore) {
            this.setData({
                page: page + 1
            })
            this.commonSearch(++page, 'bottom')
        }
    },
    commonSearch(page, type) {
        let {
            orderList,
            startTime,
            endTime
        } = this.data
        let params = {
            "page": page,
            "size": 12,
            "channel": wx.getStorageSync('phone'),
            "startTime": new Date(startTime.replace(/-/g, '/') + ' 00:00:00').getTime(),
            "endTime": new Date(endTime.replace(/-/g, '/') + ' 23:59:59').getTime()
        }
        if (type != 'pulldown') {
            wx.showLoading({
                title: '加载中',
            })
        }
        console.log(params)
        invite.channelUserDetails(params, res => {
            wx.hideLoading()
            wx.stopPullDownRefresh()
            if (res.status) {
                console.log(res.data)
                res.data.forEach(val => {
                    val.endTime = invite.formate(val.endTime)
                })
                if (res.total > orderList.length && type !== 'pulldown') {

                    orderList = [...orderList, ...res.data]

                    this.setData({
                        orderList,
                        hasMore: true,
                        loadDone: true
                    })
                } else if (type == 'pulldown') {
                    orderList = res.data
                    this.setData({
                        orderList,
                        hasMore: true,
                        loadDone: true
                    })
                } else {
                    this.setData({
                        hasMore: false,
                        loadDone: true
                    })
                }
                if (res.total == orderList.length) {
                    this.setData({
                        hasMore: false,
                        loadDone: true
                    })
                }
                if (res.total == 0) {
                    this.setData({
                        noneData: true,
                        hasMore: false,
                        loadDone: true
                    })
                }
            } else {
                wx.showToast({
                    title: res.message,
                    icon: 'none'
                })
            }
        })
    },
    bindDateChange(e) {
        let type = invite.getDataSet(e, 'type')
        let value = e.detail.value

        this.setData({
            [type + 'Time']: e.detail.value,
            page: 1,
            orderList: []
        })
        let {
            startTime,
            endTime
        } = this.data
        if (new Date(startTime).getTime() > new Date(endTime).getTime()) {
            wx.showToast({
                title: '开始时间不能大于结束时间！',
                icon: 'none'
            })
            this.setData({
                startTime: ''
            })
        } else if (startTime != '' && endTime != '') {
            this.commonSearch('1', '')
        }
    },
    onShow() {

    }

})